<template>
  <view>
    <view class="goods_list" v-for="(item, index) in goodsList" :key="index" @click="gotoGoodsDetail(item.goods_id)">
      <my-goods :goods="item"></my-goods>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        goodsList: [],
        total: 0,
        queryObj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10,
        },
        isloading: false
      }
    },
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      this.getGoodsList()
    },
    methods: {
      async getGoodsList(cb){
        this.isloading = true
        const { data: { message } } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        this.isloading = false
        cb && cb()
        this.goodsList = [...this.goodsList, ...message.goods]
        this.total = message.total
      },
       gotoGoodsDetail(id){
         uni.navigateTo({
           url: '/subpkg/goods_detail/goods_detail?goods_id=' + id
         })
       }
    },
    onReachBottom() {
      if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('到底了，别再扒拉了~')
      if(this.isloading) return
      this.queryObj.pagenum += 1
      this.getGoodsList()
    },
    onPullDownRefresh() {
      this.queryObj.pagenum = 1
      this.total = 0
      this.isloading = false
      this.goodsList = []
      this.getGoodsList(() => uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">
  
</style>
